package cn.stronglink.asset.module.space.service;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.ItAssetExtPosition;
import cn.stronglink.asset.module.space.mapper.SpaceMapper;
import cn.stronglink.asset.module.space.vo.SpaceVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class SpaceService {

	@Autowired
	private SpaceMapper spaceMapper;
	
	
	public Page<SpaceVo> queryHardList(Page<SpaceVo> page, Map<String, Object> params) {
		// 查询列表
		List<SpaceVo> spaceList =spaceMapper.queryList(page,params);
		page.setRecords(spaceList);
		return page;
	}


	public List<SpaceVo> getRoomInfo(Map<String, Object> params) {
		Integer uHeight=params.get("uHeight")==null?0:Integer.parseInt(params.get("uHeight").toString());
		List<SpaceVo> resultList=new ArrayList<SpaceVo>();
		List<SpaceVo> spaceList=spaceMapper.getRoomInfo(params);
		if(spaceList!=null&&spaceList.size()>0){
			for(SpaceVo vo:spaceList){
				int sFlag=1;
				List<ItAssetExtPosition> assetExtPositionList=spaceMapper.getItAssetExtPositionList(vo);
				if(assetExtPositionList!=null&&assetExtPositionList.size()>0){
					for(ItAssetExtPosition position:assetExtPositionList){
						if(sFlag<position.getStart()){
							SpaceVo result=new SpaceVo();
							result.setRackId(vo.getRackId());
							result.setAssetName(vo.getAssetName());
							result.setRoomName(vo.getRoomName());
							result.setStart(sFlag);
							result.setEnd(position.getStart()-1);
							result.setNums(position.getStart()-sFlag);
							if(uHeight<=position.getStart()-sFlag){
								resultList.add(result);
							}
						}
						sFlag=position.getEnd()+1;
					}
					if(sFlag<=vo.getuHeight()){
						SpaceVo result=new SpaceVo();
						result.setRackId(vo.getRackId());
						result.setAssetName(vo.getAssetName());
						result.setRoomName(vo.getRoomName());
						result.setStart(sFlag);
						result.setEnd(vo.getuHeight());
						result.setNums(vo.getuHeight()+1-sFlag);
						if(uHeight<=vo.getuHeight()+1-sFlag){
							resultList.add(result);
						}
					}
				}else{
					SpaceVo result=new SpaceVo();
					result.setRackId(vo.getRackId());
					result.setAssetName(vo.getAssetName());
					result.setRoomName(vo.getRoomName());
					result.setStart(1);
					result.setEnd(vo.getuHeight());
					result.setNums(vo.getuHeight());
					if(vo.getuHeight()!=null&&uHeight<=vo.getuHeight()){
						resultList.add(result);
					}
				}
			}
		}
		Collections.sort(resultList);
		return resultList;
	}

}
